﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;

namespace SecuritySystem.Reports
{
    public partial class InventoryReportForm : Form
    {
        TIEntities dbContext;
        public string ReportType = "";
       
        class ProductGroup
        {
            public string ProductType { get; set; }
            public string SubType { get; set; }
            public string Size { get; set; }
            public decimal Quantity { get; set; }
            public decimal TotalPrice { get; set; }
           
        }
        class InventoryReportData
        {
            public string ProductType { get; set; }
            public string SubType { get; set; }
            public string Size { get; set; }
            public decimal Quantity { get; set; }
            public decimal Price { get; set; }
        }
       
        public InventoryReportForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
            LoadMainTypes();
           
        }
        private void LoadMainTypes()
        {
           List<ProductType> productTypeList = (from pt in dbContext.ProductTypes where pt.Name != "--" select pt).Distinct().ToList();
           ProductType productType = new ProductType();
           productType.Name = "All";
           productType.ID = -1;
           productTypeList.Insert(0, productType);
            comboBoxMainType.DataSource = null;
            comboBoxMainType.DisplayMember = "Name";
            comboBoxMainType.ValueMember = "ID";
            comboBoxMainType.DataSource = productTypeList;
            comboBoxMainType.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadReport(Convert.ToInt32(comboBoxMainType.SelectedValue));
        }

        private void InventoryReportForm_Load(object sender, EventArgs e)
        {
            LoadReport(-1);

        }
        private void LoadReport(int productID)
        {
            if (ReportType == "InventoryReport")
            {
                try
                {
                    List<ProductGroup> iGroupList = new List<ProductGroup>();
                    List<ProductGroup> sGroupList = new List<ProductGroup>();

                    if (productID == -1)
                    {
                        iGroupList = (from i in dbContext.Inventories
                                                        group i by new { TypeName = i.ProductType.Name, SubTypeName = i.ProductSubType.Name, SizeName = i.ProductSize.Name}
                                                             into grp
                                                             select new ProductGroup()
                                                             {
                                                                 ProductType = grp.Key.TypeName,
                                                                 SubType = grp.Key.SubTypeName,
                                                                 Size = grp.Key.SizeName,
                                                                 Quantity = grp.Sum(t => t.Quantity),
                                                                 TotalPrice = grp.Sum(t => t.Quantity * t.Price),
                                                                 
                                                             }).ToList();



                       sGroupList = (from s in dbContext.SalesTransactions
                                     group s by new { TypeName = s.ProductType.Name, SubTypeName = s.ProductSubType.Name, SizeName = s.ProductSize.Name }
                                                             into grp
                                                             select new ProductGroup()
                                                             {

                                                                 ProductType = grp.Key.TypeName,
                                                                 SubType = grp.Key.SubTypeName,
                                                                 Size = grp.Key.SizeName,
                                                                 Quantity = grp.Sum(s => s.QUANITY.Value)
                                                              }).ToList();
                    }
                    else
                    {
                        iGroupList = (from i in dbContext.Inventories
                                                         where i.ProductTypeID == productID
                                      group i by new { TypeName = i.ProductType.Name, SubTypeName = i.ProductSubType.Name, SizeName = i.ProductSize.Name }
                                                             into grp
                                                             select new ProductGroup()
                                                             {
                                                                 ProductType = grp.Key.TypeName,
                                                                 SubType = grp.Key.SubTypeName,
                                                                 Size = grp.Key.SizeName,
                                                                 Quantity = grp.Sum(t => t.Quantity),
                                                                 TotalPrice = grp.Sum(t => t.Quantity * t.Price),
                                                             }).ToList();



                        sGroupList = (from s in dbContext.SalesTransactions
                                                         where s.PRODUCT_TYPE_ID == productID
                                      group s by new { TypeName = s.ProductType.Name, SubTypeName = s.ProductSubType.Name, SizeName = s.ProductSize.Name }
                                                             into grp
                                                             select new ProductGroup()
                                                             {

                                                                 ProductType = grp.Key.TypeName,
                                                                 SubType = grp.Key.SubTypeName,
                                                                 Size = grp.Key.SizeName,
                                                                 Quantity = grp.Sum(s => s.QUANITY.Value)
                                                             }).ToList();
                    }

                    List<InventoryReportData> reportDataList = new List<InventoryReportData>();
                    foreach (ProductGroup iProductGroup in iGroupList)
                    {
                        InventoryReportData reportData = new InventoryReportData();
                        ProductGroup sProductGroup = (from s in sGroupList
                                                      where s.ProductType == iProductGroup.ProductType &&
                                                              s.SubType == iProductGroup.SubType &&
                                                              s.Size == iProductGroup.Size
                                                      select s).FirstOrDefault();

                        if (sProductGroup == null)
                        {
                            reportData.ProductType = iProductGroup.ProductType;
                            reportData.SubType = iProductGroup.SubType;
                            reportData.Size = iProductGroup.Size;
                            reportData.Quantity = iProductGroup.Quantity;
                            reportData.Price = iProductGroup.TotalPrice;
                        }
                        else
                        {
                            reportData.ProductType = iProductGroup.ProductType;
                            reportData.SubType = iProductGroup.SubType;
                            reportData.Size = iProductGroup.Size;
                            reportData.Quantity = iProductGroup.Quantity - sProductGroup.Quantity;
                            reportData.Price = iProductGroup.TotalPrice / iProductGroup.Quantity * reportData.Quantity;
                        }
                        reportDataList.Add(reportData);
                    }
                    InventoryReport reportobj = new InventoryReport();
                    reportobj.SetDataSource(reportDataList);

                    ParameterFieldDefinitions crParameterFieldDefinitions;
                    ParameterFieldDefinition crParameterFieldDefinition;
                    ParameterValues crParameterValues = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

                    crParameterDiscreteValue.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyName"];
                    crParameterFieldDefinitions = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition = crParameterFieldDefinitions[0];
                    crParameterValues = crParameterFieldDefinition.CurrentValues;

                    crParameterValues.Clear();
                    crParameterValues.Add(crParameterDiscreteValue);
                    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

                    ParameterFieldDefinitions crParameterFieldDefinitions1;
                    ParameterFieldDefinition crParameterFieldDefinition1;
                    ParameterValues crParameterValues1 = new ParameterValues();
                    ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();

                    crParameterDiscreteValue1.Value = System.Configuration.ConfigurationSettings.AppSettings["CompanyAddress"];
                    crParameterFieldDefinitions1 = reportobj.DataDefinition.ParameterFields;
                    crParameterFieldDefinition1 = crParameterFieldDefinitions1[1];
                    crParameterValues1 = crParameterFieldDefinition1.CurrentValues;

                    crParameterValues1.Clear();
                    crParameterValues1.Add(crParameterDiscreteValue1);
                    crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);
                    this.crystalReportViewer1.ReportSource = reportobj;

                }
                catch
                {

                }
            }
        }

        private void comboBoxMainType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        
       
    }
}
